<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="Sku" prop="skuId">
      <!-- <el-input v-model="dataForm.skuId" placeholder="SkuId"></el-input> -->
      <template>
        <el-select :disabled="dataForm.id ? true:false" v-model="dataForm.skuId" @click.native="getSku()" placeholder="请选择">
          <el-option
            v-for="item in Skus"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </template>
    </el-form-item>
    <el-form-item label="仓库编号" prop="wareId">
      <!-- <el-input v-model="dataForm.ware.id" placeholder="仓库编号"></el-input> -->
      <template>
        <el-select v-model="dataForm.wareId" placeholder="请选择" @click.native="getWare()">
          <el-option
            v-for="item in Wares"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </template>
    </el-form-item>
    <el-form-item label="库存数" prop="stock">
      <el-input v-model="dataForm.stock" placeholder="库存数"></el-input>
    </el-form-item>
    <el-form-item label="锁定库存" prop="stockLocked">
      <!-- <el-input v-model="dataForm.stockLocked" placeholder="锁定库存"></el-input> -->
       <el-switch
          v-model="dataForm.stockLocked"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="true"
          :inactive-value="false"
        ></el-switch>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        wares: [],
        skus: [],
        wareSkus: [],
        dataForm: {
          id: 0,
          skuId: '',
          wareId: '',
          stock: '',
          stockLocked: true
        },
        dataRule: {
          sku: [
            { required: true, message: 'Sku不能为空', trigger: 'blur' }
          ],
          ware: [
            { required: true, message: '仓库编号不能为空', trigger: 'blur' }
          ],
          stock: [
            { required: true, message: '库存数不能为空', trigger: 'blur' }
          ],
          stockLocked: [
            { required: true, message: '锁定库存不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    computed: {
      Wares: function () {
        let wareIds = []
        this.wareSkus.filter(waresku => waresku.sku.id === this.dataForm.skuId).forEach(waresku => {
          wareIds.push(waresku.ware.id)
        })
        if (this.dataForm.id) {
          return this.wares
        } else if (this.wares.length >= 1) {
          return this.wares.filter(ware => !wareIds.includes(ware.id))
        }
      },
      Skus: function () {
        let skuIds = []
        this.wareSkus.filter(waresku => waresku.ware.id === this.dataForm.wareId).forEach(waresku => {
          skuIds.push(waresku.sku.id)
        })
        if (this.dataForm.id) {
          return this.skus
        } else {
          return this.skus.filter(sku => !skuIds.includes(sku.id))
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.skus = []
          this.wares = []
          // this.dataForm.sku = []
          // this.dataForm.ware = []
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/ware/ware_sku/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.id = data.wareSkuVo.id
                this.skus = data.wareSkuVo.sku
                this.dataForm.skuId = data.wareSkuVo.sku.id
                this.dataForm.wareId = data.wareSkuVo.ware.id
                this.wares = data.wareSkuVo.ware
                this.dataForm.stock = data.wareSkuVo.stock
                this.dataForm.stockLocked = data.wareSkuVo.stockLocked
              }
            })
          }
          this.wareSkus = []
          this.$http({
            url: this.$http.adornUrl(`/ware/ware_sku/list`),
            method: 'get',
            params: this.$http.adornParams({
              'page': 1,
              'limit': 1000,
              'key': null
            })
          }).then(data => {
            console.log(data)
            this.wareSkus = data.data.page.list
            console.log(data.data.page.list)
          })
        })
      },
      getWare () {
        this.$http({
          url: this.$http.adornUrl(`/ware/ware/list`),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 10000,
            'key': null
          })
        }).then((data) => {
          this.wares = data.data.page.list
        })
      },
      getSku () {
        this.$http({
          url: this.$http.adornUrl(`/product/sku/list`),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 10000,
            'key': null
          })
        }).then((data) => {
          this.skus = data.data.page.list
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/ware/ware_sku/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'skuId': this.dataForm.skuId,
                'wareId': this.dataForm.wareId,
                'stock': this.dataForm.stock,
                'stockLocked': this.dataForm.stockLocked
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
